﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EditData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string tableName = Request.QueryString["tableName"];
        string rowID = Request.QueryString["rowID"];
        string columnName = Request.QueryString["columnName"];
        if (tableName == null || rowID == null || columnName == null)
        {
            Response.SuppressContent = true;
            Response.StatusCode = 400;
            return;
        }
        CreateFileLink(tableName, rowID, columnName);
    }

    private void CreateFileLink(string tableName, string rowID, string columnName)
    {
        Uri url = new Uri("BinaryDataRetrival.ashx?tableName="+tableName+"&rowID="+rowID+"&columnName="+columnName,UriKind.Relative);
        LinkToData.Text = "<a href=\"" + url.ToString() + "\">Link to data</a>";
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        
        string tableName = Request.QueryString["tableName"];
        string rowID = Request.QueryString["rowID"];
        string columnName = Request.QueryString["columnName"];
        if (tableName == null || rowID == null || columnName == null)
        {
            Response.SuppressContent = true;
            Response.StatusCode = 400;
            return;
        }
        
        if (FileUpload1.HasFile)
        {
            System.IO.Stream file = FileUpload1.FileContent;
            ModdbFrontEnd frontEnd = new ModdbFrontEnd();
            try
            {
                frontEnd.AddDataToColumn(file, tableName, rowID, columnName);
            }
            catch (System.ServiceModel.EndpointNotFoundException)
            {
                Response.SuppressContent = true;
                Response.StatusCode = 400;
                return;
            }
        }
    }
}
